# 获取屏幕快照
import pyautogui as auto
import pyscreeze


def autoFindImg():
    # position = locateCenterOnScreen('MouseAuto\markImg\headImg1.png')
    # if position is not None:
    #     print("开始位置：{}，{}".format(position[0], position[1]))
    # click(x, y)
    # auto.moveTo(x, y, logScreenshot=True)

    # 多个目标图的实现
    # retVal = locateAllOnScreen('MouseAuto\markImg\headImg1.png', logScreenshot=True, confidence=0.7, grayscale=False)
    # print(retVal)
    # for position in retVal:
    #     print(position)
    #     x, y = auto.center(position)
    #     print("1图像在屏幕中的位置：{}，{}".format(x, y))

    # position = next(retVal)
    # x, y =auto.center(position)
    # print("1图像在屏幕中的位置：{}，{}".format(x, y))
    # position = next(retVal)
    # x, y =auto.center(position)
    # print("2图像在屏幕中的位置：{}，{}".format(x, y))
    # while position is None:
    #     x, y =auto.center(position)
    #     print("图像在屏幕中的位置：{}，{}".format(x, y))
    #     position = next(retVal)

    # auto.displayMousePosition()
    title = auto.getAllTitles()
    print(title)
    root = auto.getWindowsWithTitle('网易有道词典')
    print(root)
    button = auto.confirm("this is test window", "testWindow", buttons=['OK','yes','萨斯砥'])
    print(button)
    prompt = auto.prompt(text="test", title="test是test", default="1234123")
    print(prompt)
    prompt = auto.password(text="test", title="test是test", default="1234123", mask="❤")
    print(prompt)
    print("=============END=============")


def size():
    """
    获取屏幕尺寸
    """
    screenWidth, screenHeight = auto.size()
    print("屏幕尺寸信息：{}，{}".format(screenWidth, screenHeight))
    return screenWidth, screenHeight


def locateOnScreen(file, confidence):
    """
    获取图像所在屏幕位置
    是一个四元数组，可以通过 cent() 函数进行解析
    file 文件的相对路径
    confidence 是相关度
    还兼容一些其他的 opencv 参数
    """
    position = pyscreeze.locateOnScreen(file, confidence=confidence)
    print(position)


def locateAllOnScreen(file, logScreenshot=False, confidence=0.7, **kwargs):
    # logScreenshot：是否开启快照机制，保存当前方法的执行快照
    LocalPath = "C:/Users/tianfeng1598/Desktop/新建文件夹/mouse-move/MouseAuto"
    auto.screenshot(LocalPath + "/locateAllOnScreen.png")
    return auto.locateAllOnScreen('MouseAuto\markImg\headImg.png', confidence=confidence, **kwargs)


def center(position):
    """
    转换四元数组，获得一个二维坐标
    """
    if position != None:
        # 将四元数组转换为 横纵坐标
        x, y = auto.center(position)
        print("图像在屏幕中的位置：{}，{}".format(x, y))
        return x, y


def moveTo(x, y, duration=0.5):
    """
    将鼠标移动到目标位置
    duration 移动耗时，单位秒
    """
    auto.moveTo(x, y, duration=duration)


def locateCenterOnScreen(file, confidence=0.7, **kwargs):
    """
    获取图像位置的二维坐标
    file 文件的相对位置
    confidence 对比系数
    """
    # 对比系数为 0.7 时效果最好
    location = auto.locateCenterOnScreen(
        file, confidence=confidence, **kwargs)
    print(location)
    if location == None:
        print("图像在屏幕中未找到目标")
        # raise Exception("图像在屏幕中未找到目标")
        return None

    x = location[0]
    y = location[1]
    print("图像在屏幕中的位置：{}，{}".format(x, y))
    return x, y


def click(x, y, duration=0.5):
    auto.click(x, y, duration=0.5)


def scroll(up=None, down=None, right=None, left=None):
    """
    滚动，指定鼠标的四向滚动
    up 向上
    down 向下
    right 向右(拖动)
    left 向左(拖动)
    """
    if up is not None:
        auto.scroll(up)
    if down is not None:
        auto.scroll(-1 * down)
    if right is not None:
        auto.dragRel(0, right)
    if left is not None:
        auto.dragRel(0, -1 * left)


if __name__ == '__main__':
    autoFindImg()
